package com.bigdata

import scala.io.Source

object Demo21ClazzNum {

  def main(args: Array[String]): Unit = {
    /**
     * 2、统计学生的总分
     */
    //1、读取分数表
    val scoresList: List[String] = Source.fromFile("data/score.txt").getLines().toList

    //2、过滤脏数据
    val filterScore: List[String] = scoresList.filter(line => {
      val arr: Array[String] = line.split(",")
      arr.length == 3 && arr.last.toInt != 0
    })

    // filterScore.foreach(println)

    //3、取出学号和分数
    val pairScore: List[(String, Int)] = filterScore.map(line => {
      val arr: Array[String] = line.split(",")
      (arr.head, arr.last.toInt)
    })

    //4、按照学号分组
    val groupScore: Map[String, List[(String, Int)]] = pairScore.groupBy(kv => kv._1)

    //5、统计学生的总分
    val result: Map[String, Int] = groupScore.map(kv => {
      val list: List[(String, Int)] = kv._2
      // 拿到所有分数的集合
      val scores: List[Int] = list.map(kv => kv._2)
      (kv._1, scores.sum)
    })

    result.foreach(println)


  }
}
